$(function () {
    // 轮播图
    var mySwiper = new Swiper('.swiper-container', {
        // direction: 'vertical', // 垂直切换选项
        loop: true, // 循环模式选项


        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },

        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        // 图片切换的时间
        autoplay: {
            delay: 3000,
        },
        // 如果需要滚动条
        // scrollbar: {
        //     el: '.swiper-scrollbar',
        // },
    })
    var page = 1;
    var row = 2;
    loadData();
    function loadData() {
        $.ajax({
            url: "http://localhost:8848/getborrow.php",
            type: "GET",
            data: {
                page: page,//当前页面
                row: row //每页条数
            },
            dataType: "JSON",//数据如果是字符串形式就不需要这行代码，如果是对象那么就需要这行代码
            success: function (data) {
                var total = data.total;//获取总记录数
                var pageSize = Math.ceil(total / row);      //得总也数
                console.log(pageSize);
                $('#page').pagenation({
                    nowPage: page,//当前页
                    pageNum: pageSize,//总页数
                    callback: function (p) {//回调函数
                        page = p//改变全局变量 当前页的数据
                        loadData();//重新调用加载数据的函数
                    }
                })
                console.log(data);
                var lists = data.list;//数据[{},{}...]
                len = lists.length;

                var resHtml = "";
                for (i = 0; i < len; i++) {
                    // console.log(lists[i]);
                    resHtml += "<tr>";
                    resHtml += "<td>" + lists[i].userid + "</td>";
                    resHtml += "<td>" + lists[i].title + "</td>";
                    resHtml += "<td>" + (Number(lists[i].interest)).toFixed(2) + "%" + "</td>";
                    resHtml += "<td>" + lists[i].borrowmoney + "元" + "</td>";
                    resHtml += "<td>" + [lists[i].repaytype == 0 ? "按月分期" : " 到月到期"] + "</td>";
                    resHtml += "<td>" + ((lists[i].ownmoney / lists[i].borrowmoney) * 100).toFixed(2) + "%" + "</td>";
                    resHtml += "<td>"
                    resHtml += "<a href='#' class='btn btn-danger btn-sm' data-borrowid='" + lists[i].id + "'>查看</a>"
                    resHtml += "</td>"
                    resHtml += "</tr>"
                }
                $(" #borrow-list").html(resHtml);

            }
        })
    }

    // 给借款列表（查看按钮）添加事件
    $('#borrow-list').on('click', 'a.btn', function () {
        var borrowid = $(this).data('borrowid');
        sessionStorage.setItem("borrowid", borrowid);
        location.href = "/borrow_info.html";
        return false;
    })

})


/*
         {
        "id": "7",
        "userid": "1",
        "borrowmoney": "10000",
        "interest": "10",
        "minbid": "10",
        "days": "10",
        "title": "我来借钱",
        "info": "借款描述",
        "borrowtime": "12",
        "repaytype": "1",
        "ownmoney": "1000",
        "bouns": "1"
        */
